<?php
namespace app\frontend\controller;
use think\Db;
use think\Controller;
class Order extends Controller
{
    public function index()
    {
      if(!session('user_name'))
        {
            $user_name='登录';
            $user_register='注册';
            $user_logout='';
        }
        else
        {
        $user_name=session('user_name');
        $user_register='欢迎你';
        $user_logout='退出';
        }

        $this->assign('user_name',$user_name);
        $this->assign('user_register',$user_register);
        $this->assign('user_logout',$user_logout);

      $user_id=session('user_id');
      $user_score=db('user')->where("user_id='{$user_id}'")->field('user_score')->select();
      $price_total=$_POST['priceTotal1'];
      
     
         foreach ($user_score as $val) 
        { 
          $user_score=$val['user_score'];
        }

        if($user_score>=50 and $user_score<100)
        {
          $priceTotal=$price_total*0.95;
          $user_score='9.5折优惠';

        } 
        else if ($user_score>=100 and $user_score<150) {
          $priceTotal=$$price_total*0.9;
          $user_score='9折优惠';
        }
        else if ($user_score>=150) {
          $priceTotal=$$price_total*0.8;
          $user_score='8折优惠';
        }
        else
        {
          $priceTotal=$price_total;
          $user_score='暂无优惠';
        }

      $order_number = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
      

      //array_filter($shopcart_id);
    	$shopcart_id=$_POST['productId'];
    	$shopcartId=explode(',', $shopcart_id);
      $text=[];

       for($i=0;$i<count($shopcartId);$i++)
      {
        if($shopcartId[$i]!='')
        {   
          $shopcartNewid=$shopcartId[$i];
          $data=db('product')->alias('a')->join('shopcart b','a.product_number=b.product_number')->where("shopcart_id={$shopcartNewid}")->select();
          $change=db('shopcart')->where("shopcart_id={$shopcartNewid}")->update(['status'=>'1']);

           foreach ($data as $val) 
            {
               $list=$val;  
               $text[]=$list;
            }
         }
      }
      $ret=db('user')->where("user_id={$user_id}")->select();
    
      $this->assign('ret',$ret);
    	$this->assign('text',$text);
      $this->assign('priceTotal',$priceTotal);
      $this->assign('price_total',$price_total);
      $this->assign('order_number',$order_number);
      $this->assign('shopcart_id',$shopcart_id);
      $this->assign('user_score',$user_score);
      //$this->assign('val_product_name',$val_product_name);
    	return view('./index/order');
    }
   
}